package com.lmm.lcbusiness.lcbusiness.pages.richtxt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lmm.lcbusiness.lcbusiness.R;
import com.lmm.lcbusiness.lcbusiness.config.AppImgUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xmgong on 2017/6/1.
 * 富文本显示容器
 */
public class RichTxtView extends LinearLayout {
    LinearLayout mLinearLayout;
    List<RichTxtModel> mRichTxtModelList = new ArrayList<>();
    LayoutParams mLayoutParams;

    public RichTxtView(Context context) {
        super(context);
        initView();
    }

    public RichTxtView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mLayoutParams.setMargins(30, 15, 30, 15);
        LayoutInflater.from(getContext()).inflate(R.layout.view_richtxt, this, true);
        mLinearLayout = (LinearLayout) findViewById(R.id.lin);
    }

    public void setRichTxtModelList(List<RichTxtModel> richTxtModelList) {
        mRichTxtModelList = richTxtModelList;
        mLinearLayout.removeAllViews();
        for (RichTxtModel rtm : mRichTxtModelList) {
            if (rtm.getRichType() == RichTxtModel.RichType_Image) {
                addImageView(rtm);
            } else if (rtm.getRichType() == RichTxtModel.RichType_Txt) {
                addTextView(rtm);
            }
        }
    }

    private void addImageView(RichTxtModel rtm) {
        ImageView iv = new ImageView(getContext());
        iv.setLayoutParams(mLayoutParams);
        iv.setAdjustViewBounds(true);
        Picasso.with(getContext()).load(AppImgUtils.getAppImgUrl(rtm.getRichValue())).into(iv);
        mLinearLayout.addView(iv);
    }

    private void addTextView(RichTxtModel rtm) {
        TextView tv = new TextView(getContext());
        tv.setLayoutParams(mLayoutParams);
        tv.setTextSize(16);
        tv.setText(rtm.getRichValue());
        mLinearLayout.addView(tv);
    }
}
